#coding:utf-8
from django.shortcuts import render_to_response
from django.http import HttpResponse
from djstu.books.models import Book

def search_form(request):
    '''直接显示静态文件'''
    return render_to_response('search_form.html')

def search(request):
    '''查询'''
    errors = []
    if 'q' in request.GET:
       q = request.GET['q']
       if not q:
           errors.append('Enter a search term.')
       elif len(q) > 20:
           errors.append('Plase enter at most 20 characters.')
       else:
           books = Book.objects.filter(title__icontains=q)
           return render_to_response('search_results.html',
                                 {'books':books, 'query':q})

    return render_to_response('search_form.html',{'errors':errors})
